﻿using Aker.DevTeam;
using System;
using System.Globalization;
using System.IO;

namespace Aker
{
    public partial class Settings : XPopupForm
    {
        public Settings()
        {
            InitializeComponent();
        }

        static FileInfo fi = new FileInfo("c:\\Aker\\Aker.txt");

        public static string FairNo;
        public static string FairName;
        public static string Season;
        public static string Currency;
        //public static int PriceType = 0;
        public static string Mail_Showroom_TR_Subject;
        public static string Mail_Showroom_TR_Body;
        public static string Mail_Showroom_EN_Subject;
        public static string Mail_Showroom_EN_Body;
        public static string Mail_Fair_TR_Subject;
        public static string Mail_Fair_TR_Body;
        public static string Mail_Fair_EN_Subject;
        public static string Mail_Fair_EN_Body;
        public static decimal DollarEuroParity;
        public static string TerminalNo;

        public static string BlackComPort;
        public static string BlackNewComPort;
        public static string BlueComPort;


        public static void Init()
        {
            string[] lines = File.ReadAllLines(fi.FullName);

            Data.TAM.SettingTableAdapter.Fill(Data.DB.Setting);

            FairNo = Data.GetSetting(1);
            FairName = Data.GetSetting(2);
            Season = Data.GetSetting(3);
            Currency = Data.GetSetting(4);

            Mail_Showroom_TR_Subject = Data.GetSetting(5);
            Mail_Showroom_TR_Body = Data.GetSetting(6);
            Mail_Showroom_EN_Subject = Data.GetSetting(7);
            Mail_Showroom_EN_Body = Data.GetSetting(8);

            Mail_Fair_TR_Subject = Data.GetSetting(9);
            Mail_Fair_TR_Body = Data.GetSetting(10);
            Mail_Fair_EN_Subject = Data.GetSetting(11);
            Mail_Fair_EN_Body = Data.GetSetting(12);

            DollarEuroParity = GetDecimal(Data.GetSetting(13));


            foreach (string line in lines)
            {
                string clearedLine = line.Trim().Replace("\t", "");

                if (clearedLine.StartsWithCI("TerminalNo"))
                    TerminalNo = clearedLine.ExtractValue("TerminalNo");

                else if (clearedLine.StartsWithCI("BlackComPort"))
                    BlackComPort = clearedLine.ExtractValue("BlackComPort");

                else if (clearedLine.StartsWithCI("BlackNewComPort"))
                    BlackNewComPort = clearedLine.ExtractValue("BlackNewComPort");

                else if (clearedLine.StartsWithCI("BlueComPort"))
                    BlueComPort = clearedLine.ExtractValue("BlueComPort");
            }

            //PriceType = Data.GetSetting(15).ToIntNN();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            this.Text = Language.MainForm.Settings;
            lbl1.Text = Language.Settings.FairEmail;
            lbl2.Text = Language.Settings.ShowroomMail;
            lbl3.Text = Language.Settings.FairStandNo;
            lblFair.Text = Language.Settings.Fair;
            lblSeason.Text = Language.Settings.Season;
            lblCurrency.Text = Language.Settings.Currency;
            lblParity.Text = Language.Settings.Parity;
            lblPriceType.Text = Language.Settings.PriceType;
            btnSave.Text = Language.Save;


            //if (!AppSession.IsOnline)
            //{
            //    txtFairNo.Enabled = false;
            //    txtFair.Enabled = false;
            //    txtSeason.Enabled = false;
            //    txtCurrency.Enabled = false;
            //    txtDollarEuroParity.Enabled = false;
            //}

            txtFairNo.Text = FairNo;
            txtFair.Text = FairName;
            txtSeason.Text = Season;
            txtCurrency.Text = Currency;
            txtDollarEuroParity.Text = DollarEuroParity.ToString();

            txtTerminalNo.Text = TerminalNo;

            txtBlackComPort.Text = BlackComPort;
            txtBlueComPort.Text = BlueComPort;
            txtBlackNewComPort.Text = BlackNewComPort;
            //chkPrices.SelectedIndex = PriceType.ToIntNN();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FairNo = txtFairNo.Text;
            FairName = txtFair.Text;
            Season = txtSeason.Text;
            Currency = txtCurrency.Text;
            //PriceType = chkPrices.SelectedIndex;


            decimal newDollarEuroParity = GetDecimal(txtDollarEuroParity.Text);

            if (DollarEuroParity != newDollarEuroParity)
            {
                DollarEuroParity = newDollarEuroParity;
                Data.DM.ExecuteNonQuery("UpdEuroPrices", Settings.DollarEuroParity);
            }

            TerminalNo = txtTerminalNo.Text;
            BlackComPort = txtBlackComPort.Text;
            BlueComPort = txtBlueComPort.Text;
            BlackNewComPort = txtBlackNewComPort.Text;

            string settings = "";

            settings += "TerminalNo\t" + txtTerminalNo.Text + Environment.NewLine;
            settings += "BlackComPort\t" + txtBlackComPort.Text + Environment.NewLine;
            settings += "BlueComPort\t" + txtBlueComPort.Text + Environment.NewLine;
            settings += "BlackNewComPort\t" + txtBlackNewComPort.Text + Environment.NewLine;

            File.WriteAllText(fi.FullName, settings);

            // if (AppSession.IsOnline)
            {
                Data.SetSetting(1, FairNo);
                Data.SetSetting(2, FairName);
                Data.SetSetting(3, Season);
                Data.SetSetting(4, Currency);

                Data.SetSetting(5, Mail_Showroom_TR_Subject);
                Data.SetSetting(6, Mail_Showroom_TR_Body);
                Data.SetSetting(7, Mail_Showroom_EN_Subject);
                Data.SetSetting(8, Mail_Showroom_EN_Body);

                Data.SetSetting(9, Mail_Fair_TR_Subject);
                Data.SetSetting(10, Mail_Fair_TR_Body);
                Data.SetSetting(11, Mail_Fair_EN_Subject);
                Data.SetSetting(12, Mail_Fair_EN_Body);

                Data.SetSetting(13, DollarEuroParity.ToString());
                //Data.SetSetting(15, PriceType.ToString());

                Data.TAM.SettingTableAdapter.Update(Data.DB.Setting);
            }

            this.Hide();
        }

        private static decimal GetDecimal(string text)
        {
            return decimal.Parse(text.Replace(",", CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator), NumberStyles.Any, CultureInfo.InvariantCulture);
        }



        private void btnFairTR_Click(object sender, EventArgs e)
        {
            using (HtmlEditor editor = new HtmlEditor("Fuar | e-posta Şablonu", "e-posta Konu", Mail_Fair_TR_Body, Mail_Fair_TR_Subject))
            {
                editor.ShowDialog();
                Mail_Fair_TR_Subject = editor.Subject;
                Mail_Fair_TR_Body = editor.HTML;
            }
        }

        private void btnFairEN_Click(object sender, EventArgs e)
        {
            using (HtmlEditor editor = new HtmlEditor("FAIR | e-mail Template", "e-mail Subject", Mail_Fair_EN_Body, Mail_Fair_EN_Subject))
            {
                editor.ShowDialog();
                Mail_Fair_EN_Subject = editor.Subject;
                Mail_Fair_EN_Body = editor.HTML;
            }
        }

        private void btnShowroomTR_Click(object sender, EventArgs e)
        {
            using (HtmlEditor editor = new HtmlEditor("Showroom | e-posta Şablonu", "e-posta Konu", Mail_Showroom_TR_Body, Mail_Showroom_TR_Subject))
            {
                editor.ShowDialog();
                Mail_Showroom_TR_Subject = editor.Subject;
                Mail_Showroom_TR_Body = editor.HTML;
            }
        }

        private void btnShowroomEN_Click(object sender, EventArgs e)
        {
            using (HtmlEditor editor = new HtmlEditor("Showeoom | e-mail Template", "e-mail Subject", Mail_Showroom_EN_Body, Mail_Showroom_EN_Subject))
            {
                editor.ShowDialog();
                Mail_Showroom_EN_Subject = editor.Subject;
                Mail_Showroom_EN_Body = editor.HTML;
            }
        }
    }
}
